package com.gec.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gec.domain.User;
import com.gec.service.UserService;
import com.gec.service.impl.UserServiceImpl;

@WebServlet("/login.action")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		/*
		 * 1 获取页面传递的用户名，密码
		 * 2生成userservice对象
		 * 3调用login的方法并返回 用户
		 * 4如果用户不为空，输出，如果空，就提示
		 */
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		System.out.println("name:" + name + ",password:" + password);
		
		UserService service = new UserServiceImpl();
		User user = service.login(name,password);
		
		if(user !=null) {
			out.println("欢迎" + user.getName()+ " 光临");
			
			//把用户信息存入session
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			
			response.sendRedirect("usercontrol.action");
		}else {
			out.println("用户名或密码不正确<br/><a href='login.html>重新登录</a>");
		}
	}

}
